<!-- 专注力测评用户编辑弹窗 -->
<template>
  <el-dialog
    :destroy-on-close="true"
    :lock-scroll="false"
    :title="isUpdate?'修改专注力测评用户':'添加专注力测评用户'"
    :visible="visible"
    width="550px"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">

      <el-form-item
        label="姓名:"
        prop="name">
        <el-input
          v-model="form.name"
          :maxlength="20"
          clearable
          placeholder="请输入姓名"/>
      </el-form-item>

      <el-form-item label="性别:" prop="gender">
        <el-select
          v-model="form.gender"
          class="ele-block"
          clearable
          placeholder="请选择性别">
          <el-option :value="1" label="男"/>
          <el-option :value="2" label="女"/>
        </el-select>
      </el-form-item>

      <el-form-item
        label="生日:"
        prop="birth">
        <el-date-picker
          style="width: 100%"
          v-model="form.birth"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="请选择出生日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="年龄:" prop="age" v-show="false">
        <el-input-number
          v-model="form.age"
          :min="0"
          class="ele-fluid ele-text-left"
          controls-position="right"
          :disabled="true"
          placeholder="请输入年龄"/>
      </el-form-item>

      <el-form-item
        label="联系方式:"
        prop="phone">
        <el-input
          v-model="form.phone"
          :maxlength="20"
          clearable
          placeholder="请输入联系方式"/>
      </el-form-item>

      <el-form-item
        label="备注:">
        <el-input
          type="textarea"
          :rows="5"
          v-model="form.remark"
          clearable
          placeholder="请输入备注"/>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        :loading="loading"
        type="primary"
        @click="save">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

export default {
  name: 'AttentionUserEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {},
  data() {
    return {
      // 表单数据
      form: Object.assign({status: 1}, this.data),
      // 表单验证规则
      rules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ],

        gender: [
          {required: true, message: '请选择性别', trigger: 'blur'}
        ],
        birth: [
          {required: true, message: '请输入生日', trigger: 'blur'}
        ],
        age: [
          {required: true, message: '请输入年龄', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入联系方式', trigger: 'blur'}
        ],
        remark: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    },
    'form.birth': function (newValue) {
      // 当birthdate变化时，计算年龄
      this.updateAge(newValue);
    }
  },
  methods: {
    updateAge(birthdate) {
      if (!birthdate) {
        this.age = '请输入出生日期';
        return;
      }
      const today = new Date();
      const birthDateObj = new Date(birthdate);
      let age = today.getFullYear() - birthDateObj.getFullYear();
      const monthDiff = today.getMonth() - birthDateObj.getMonth();
      if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDateObj.getDate())) {
        age--;
      }
      this.form.age = age;
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/attentionuser/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
